﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Text;
using EuroManager.Domain.Simulator.Helpers;

namespace EuroManager.Domain.Simulator.Engine
{
    public class Timer : ITimeAware
    {
        private readonly float time;
        private float currentTime = 0;

        public Timer(float time, float salt)
        {
            Contract.Requires(time.IsRational() && time > 0);
            Contract.Requires(salt.IsRational());

            this.time = time;

            currentTime = salt;
        }

        public Timer(float time)
            : this(time, 0)
        {
        }

        public bool HasPassed
        {
            get { return currentTime >= time; }
        }

        public void Update(float elapsedTime)
        {
            currentTime += elapsedTime;
        }

        public void Reset(float salt)
        {
            Contract.Requires(salt.IsRational());

            currentTime = salt;
        }

        public void Reset()
        {
            Reset(0);
        }

        public void Reduce(float salt)
        {
            Contract.Requires(salt.IsRational());

            currentTime -= time;
            currentTime += salt;
        }

        public void Reduce()
        {
            Reduce(0);
        }
    }
}
